package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.EmployeeInfoMapper;
import com.suyuan.dao.PermissionEmployeeMapper;
import com.suyuan.dao.PermissionMapper;
import com.suyuan.entity.*;
import com.suyuan.service.PermissionEmployeeService;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by yang on 2018/3/6.
 */
@Service(value = "permissionEmployeeService")
public class PermissionEmployeeServiceImpl implements PermissionEmployeeService {
    @Resource
    private PermissionEmployeeMapper permissionEmployeeMapper;
    @Resource
    private EmployeeInfoMapper employeeInfoMapper;
    @Resource
    private PermissionMapper permissionMapper;

    public PageInfo list(Integer empId, Integer perId, Page page) {
        PermissionEmployeeExample example = new PermissionEmployeeExample();
        PermissionEmployeeExample.Criteria criteria = example.createCriteria();
        if(empId != null){
            criteria.andEidEqualTo(empId);
        }
        if(perId != null){
            criteria.andPidEqualTo(perId);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<PermissionEmployee> pes = permissionEmployeeMapper.selectByExample(example);
        PageInfo info = new PageInfo(pes);
        return info;
    }

    public int delete(int id) {
        return permissionEmployeeMapper.deleteByPrimaryKey(id);
    }

    public int save(PermissionEmployee pe) {
        return permissionEmployeeMapper.insert(pe);
    }

    public List<EmployeeInfo> getEmployees() {
        return employeeInfoMapper.selectByExample(new EmployeeInfoExample());
    }

    public List<Permission> getPermissions() {
        return permissionMapper.selectByExample(new PermissionExample());
    }

    public int insertSelective(PermissionEmployee pe) {
        return permissionEmployeeMapper.insertSelective(pe);
    }

    public List<PermissionEmployee> findByEmpId(Integer id) {
        PermissionEmployeeExample example = new PermissionEmployeeExample();
        PermissionEmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEidEqualTo(id);
        List<PermissionEmployee> permissionEmployees = permissionEmployeeMapper.selectByExample(example);
        return permissionEmployees;
    }

    public void deleteByEmpId(Integer gid) {
        PermissionEmployeeExample example = new PermissionEmployeeExample();
        PermissionEmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEidEqualTo(gid);
        permissionEmployeeMapper.deleteByExample(example);
    }
}
